/*
 * Saphre - Suffix Arrays for Phrase Extraction
 * Copyright (C) 2013 
 * Dale Gerdemann - Tübingen, Germany 
 * Niko Schenk - Frankfurt am Main, Germany
 * All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 *
 */
package saphre.commands;

import collectors.api.Collector1;
import collectors.impl.GeneralRepeatsCollector;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.regex.Matcher;
import saphre.core.Store;
import saphre.core.SuffixArray;
import util.Interval;
import collectors.impl.visitors.CollectTraversal;
import saphre.core.SaphreBase;

/**
 * Program to extract repeated n-grams from a corpus.
 * This class handles user-specified parameter settings
 * and calls the respective methods to extract the repeats.
 *
 * @author niko
 */
public class CmdRepeats {
    
    // Default values for min length of n-grams, etc.
    CmdRepeatsParameters repeatsParameters = new CmdRepeatsParameters();
    
    PrintWriter writer = null;

    /**
     * Extract repeats based on user-specific parameter settings.
     * @param store
     * @param sa
     * @param pa
     * @param top
     * @param m 
     */
    public void extractRepeats(Store store, SuffixArray sa, SuffixArray pa, Interval top, Matcher m) {
        
        getUserSpecificParameters(m);
        
        String columnNames = "ngram\tisMax\tisSmax\tlength\ttf\tdf\tmiY&C\tmiGeneral\tridf\n";
        try {
            if(repeatsParameters.isWriteToFile()) {
                writer = new PrintWriter("show_repeats.txt");
                writer.write(columnNames);
            }
            if(repeatsParameters.isWriteToConsole()) {
                System.out.print(columnNames);
            }
        } catch (FileNotFoundException ex) {
        
        }
        
        // Top down traversal of the lcp interval tree to find general repeats.
        Collector1 collect = new GeneralRepeatsCollector
                (store, sa, writer, repeatsParameters);
        CollectTraversal p =
                new CollectTraversal(store, sa, collect, top);
        p.traverse();
        
        if(repeatsParameters.isWriteToFile()) {
            writer.flush();
            writer.close(); 
        }
    }
    
    
    /**
     * Extract repeats based on user-specific parameter settings.
     * 
     * @param saphreBase
     * @param repeatsParameters 
     */
    public void extractRepeats(SaphreBase saphreBase, CmdRepeatsParameters repeatsParameters) {
        String columnNames = "ngram\tisMax\tisSmax\tlength\ttf\tdf\tmiY&C\tmiGeneral\tridf\n";
        try {
            if(repeatsParameters.isWriteToFile()) {
                writer = new PrintWriter("show_repeats.txt");
                writer.write(columnNames);
            }
            if(repeatsParameters.isWriteToConsole()) {
                System.out.print(columnNames);
            }
        } catch (FileNotFoundException ex) {
        
        }
        
        // Top down traversal of the lcp interval tree to find general repeats.
        Collector1 collect = new GeneralRepeatsCollector
                (saphreBase.getStore(), saphreBase.getSa(), writer, repeatsParameters);
        CollectTraversal p =
                new CollectTraversal(saphreBase.getStore(), saphreBase.getSa(), collect, saphreBase.getTop());
        p.traverse();
        
        if(repeatsParameters.isWriteToFile()) {
            writer.flush();
            writer.close(); 
        }
    }
    

    /*
     * 
     */
    private void getUserSpecificParameters(Matcher m) {
        // Get user input.
        
        // Min length of n-gram.
        if(m.group(1) != null) {
            int minLength = Integer.parseInt(m.group(2));
            repeatsParameters.setMinLength(minLength);
        }
        // Max length of n-gram.
        if(m.group(3) != null) {
            int maxLength = Integer.parseInt(m.group(4));
            repeatsParameters.setMaxLength(maxLength);
        }
        // Min term frequency.
        if(m.group(5) != null) {
            int minTermFrequency = Integer.parseInt(m.group(6));
            repeatsParameters.setMinTermFrequency(minTermFrequency);
        }
        // Min mutual information (general computation).
        if(m.group(7) != null) {
            double minMutualInformationYamamotoChurch = Double.parseDouble(m.group(8));
            repeatsParameters.setMinMutualInformationYamamotoChurch(minMutualInformationYamamotoChurch);
        }
        // Min mutual information (Yamamoto & Church style computation).
        if(m.group(10) != null) {
            double minMutualInformationGeneral = Double.parseDouble(m.group(11));
            repeatsParameters.setMinMutualInformationGeneral(minMutualInformationGeneral);
        }
        // Min ridf.
        if(m.group(13) != null) {
            double minResidualInverseDocumentFrequency = Double.parseDouble(m.group(14));
            repeatsParameters.setMinResidualInverseDocumentFrequency(minResidualInverseDocumentFrequency);
        }
        // Starts with term.
        if(m.group(16) != null) {
            String startsWith = m.group(17).toString();
            repeatsParameters.setStartsWith(startsWith);
        }
        // Contains term.
        if(m.group(18) != null) {
            String containsTerm = m.group(19).toString();
            repeatsParameters.setContainsTerm(containsTerm);
        }
        // Write to file.
        if(m.group(20) != null) {
            boolean writeToFile = Boolean.parseBoolean(m.group(21));
            repeatsParameters.setWriteToFile(writeToFile);
        }
        // Write to console (standard output stream).
        if(m.group(22) != null) {
            boolean writeToConsole = Boolean.parseBoolean(m.group(23));
            repeatsParameters.setWriteToConsole(writeToConsole);
        }
    }

    
}
